﻿using BW.EatRabbit.API.Application.Commands.Slaughter;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers.Slaughter
{
    public class SlaughterCreateCommandHandler : IRequestHandler<SlaughterCreateCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Slaughter_Information> _slaughterRepository;
        public SlaughterCreateCommandHandler(IBaseRepository<Slaughter_Information> slaughterRepository)
        {

            _slaughterRepository = slaughterRepository;
        }
        public Task<APIResult<int>> Handle(SlaughterCreateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            Slaughter_Information slaughter = new Slaughter_Information();
            slaughter.ProductionBatch = request.ProductionBatch;
            slaughter.ArrivalWeight = request.ArrivalWeight;
            slaughter.ArrivalTime = request.ArrivalTime;
            slaughter.FeedTime = request.FeedTime;
            slaughter.Creator = request.UserNickname;

            _slaughterRepository.AddEntity(slaughter);
            result.Code = ResultCode.添加成功;
            result.Message = ResultCode.添加成功.ToString();

            return Task.FromResult(result);
        }
    }
}
